﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;
using System.Data;
using System.Web;

namespace Joyintech.Admin.inpiao365.general.node
{
    public partial class edit : System.Web.UI.Page
    {
        #region 页面加载

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //加载默认编辑器
                this.litContent.Text = Global.GetTextEditor(this.Content.ID.ToString());

                this.AddUserName.Text = systemUser_BLL.GetInstance().GetCurrentSystemUser().UserName.ToString();
                this.AddDate.Value = DateTime.Now;

                //绑定栏目（如果是修改栏目则需排除其自身及其下级栏目）
                DataBindsystemNode();

                //加载栏目模板
                DataBindChannelTemplate();

                //加载内容模板
                DataBindContentTemplate();

                //判断是否为修改操作
                if (DNTRequest.GetQueryString("node_id") != "")
                {
                    DataBindForm();

                    this.btnSavaAs.Visible = true;
                }
                else
                {
                    this.btnSavaAs.Visible = false;
                }
            }
        }

        #endregion

        #region 绑定栏目（如果是修改栏目则需排除其自身及其下级栏目）

        /// <summary>
        /// 绑定所有栏目
        /// </summary>
        private void DataBindsystemNode()
        {
            this.ddlParentID.Items.Clear();

            this.ddlParentID.Items.Add(new ListItem("作为一级栏目", "0"));

            List<systemNode> list = new List<systemNode>();

            if (DNTRequest.GetQueryString("node_id") != "")
            {
                list = new systemNode_BLL().GetModelList("ParentID=0 and NodeID<>" + DNTRequest.GetQueryString("node_id") + " order by NodeOrder");
            }
            else
            {
                list = new systemNode_BLL().GetModelList("ParentID=0" + " order by NodeOrder");
            }

            foreach (systemNode model in list)
            {
                this.ddlParentID.Items.Add(new ListItem(model.NodeName, model.NodeID.ToString()));

                //递归绑定下级栏目
                DataBindsystemNodeChild(model.NodeID, "--");
            }
        }

        /// <summary>
        /// 递归绑定下级栏目
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindsystemNodeChild(int ParentID, string frontStr)
        {
            List<systemNode> list = new List<systemNode>();

            if (DNTRequest.GetQueryString("node_id") != "")
            {
                list = new systemNode_BLL().GetModelList("ParentID=" + ParentID + " and NodeID<>" + DNTRequest.GetQueryString("node_id") + " order by NodeOrder");
            }
            else
            {
                list = new systemNode_BLL().GetModelList("ParentID=" + ParentID + " order by NodeOrder");
            }

            foreach (systemNode model in list)
            {
                this.ddlParentID.Items.Add(new ListItem(frontStr + model.NodeName, model.NodeID.ToString()));

                //递归绑定下级栏目
                DataBindsystemNodeChild(model.NodeID, "--" + frontStr);
            }
        }

        #endregion

        #region 加载栏目模板

        /// <summary>
        /// 加载栏目模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindChannelTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='ChannelTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.ddlChannelTemplateID.Items.Add(item);
                }
            }
        }

        #endregion

        #region 加载内容模板

        /// <summary>
        /// 加载内容模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindContentTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='ContentTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.ddlContentTemplateID.Items.Add(item);
                }
            }
        }

        #endregion

        #region 绑定表单数据

        /// <summary>
        /// 绑定表单数据
        /// </summary>
        private void DataBindForm()
        {
            DataSet ds = new DataSet();

            ds = new systemNode_BLL().GetList("NodeID=" + DNTRequest.GetQueryString("node_id"));

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Joyintech.WebControls.HFPanel.BindForm(this.HFPanel1, ds.Tables[0].DefaultView);

                this.Content.Value = ds.Tables[0].Rows[0]["NodeContent"].ToString();
            }
        }

        #endregion

        #region 保存表单数据

        /// <summary>
        /// 保存表单数据到表systemNode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            systemNode model = new systemNode();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //栏目内容正文
            model.NodeContent = this.Content.Value;

            //操作结果
            int dataResult = 0;

            if (DNTRequest.GetQueryString("node_id") != "")
            {
                model.NodeID = Convert.ToInt32(DNTRequest.GetQueryString("node_id").ToString());

                dataResult = new systemNode_BLL().Update(model);
            }
            else
            {
                dataResult = new systemNode_BLL().Add(model);
            }

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "parent.location.href=parent.location.href;");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");                
            }
        }

        #endregion

        #region 保存副本表单数据

        /// <summary>
        /// 保存表单数据到表systemNode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveAs_Click(object sender, EventArgs e)
        {
            systemNode model = new systemNode();

            string strError = string.Empty;

            strError = Joyintech.WebControls.HFPanel.FormCheck(this.HFPanel1, model);

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            Joyintech.WebControls.HFPanel.GetForm(this.HFPanel1, model);

            //栏目内容正文
            model.NodeContent = this.Content.Value;

            //操作结果
            int dataResult = 0;

            //保存副本即为创建一个新的
            dataResult = new systemNode_BLL().Add(model);

            if (dataResult >= 1)
            {
                MessageBox.ShowMsgScript(this, "保存成功！", "parent.location.href=parent.location.href;");
            }
            else
            {
                MessageBox.Show(this, "保存时发生异常，请重试！");
            }
        }

        #endregion
    }
}